﻿@using Gurux.DLMS.AMI.Client.Shared
@using Gurux.DLMS.AMI.Module;
@using Gurux.DLMS.AMI.Shared.DIs

@inject GXModuleService modules
@inject IGXNotifier2 Notifier
@inject NavigationManager NavigationManager
@implements IDisposable

<CascadingValue Value=this>
    @ChildContent
</CascadingValue>
<div class="btn-group" role="group">
    @foreach (GXMenuItem it in Notifier.Items)
    {
        <button class="btn button" onclick="@(() => it.OnClick())">
            @if (it.Icon != null)
            {
                <div class="@it.Icon"></div>
            }
            &nbsp;@it.Text
        </button>
    }
</div>
<!--Show help.-->
<a href="@GetHelpPath()" target="_blank"><div class="oi oi-question-mark"></div></a>
@code {
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    ///
    string? _page;

    private string GetHelpPath()
    {
        string str = "https://gurux.fi/gurux.dlms.ami4";
        if (!string.IsNullOrEmpty(_page))
        {
            if (_page.StartsWith("https://") ||
                _page.StartsWith("http://"))
            {
                str = _page;
            }
            else
            {
                str += "/" + _page;
            }
        }
        return str;
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        string path = NavigationManager.ToBaseRelativePath(e.Location);
        int start = 0;
        if (path.ToLower().StartsWith("config/"))
        {
            start = 7;
        }
        //Find extension help.
        if (path.ToLower().StartsWith("extension/edit/"))
        {
            path = path.Substring(15);
            foreach (var asm in modules.Assemblies)
            {
                foreach (var type in asm.GetExportedTypes())
                {
                    if (type.IsAbstract || !type.IsClass || type.FullName == null)
                    {
                        continue;
                    }
                    if (type.GetInterfaces().Contains(typeof(IAmiModule)))
                    {
                        IAmiModule? tmp = (IAmiModule?)asm.CreateInstance(type.FullName);
                        if (string.Compare(path, tmp?.Id, true) == 0)
                        {
                            _page = tmp.Help;
                            StateHasChanged();
                            return;
                        }
                    }
                }
            }
            foreach (var type in modules.Types)
            {
                if (type.IsAbstract || !type.IsClass || type.FullName == null)
                {
                    continue;
                }
                if (type.GetInterfaces().Contains(typeof(IAmiModule)))
                {
                    IAmiModule? tmp = (IAmiModule?)Activator.CreateInstance(type);
                    if (string.Compare(path, tmp?.Id, true) == 0)
                    {
                        _page = tmp.Help;
                        StateHasChanged();
                        return;
                    }
                }
            }
        }
        int pos = path.IndexOf('/', start);
        if (pos != -1)
        {
            path = path.Substring(0, pos);
        }
        _page = path;
        StateHasChanged();
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Notifier.OnUpdateButtons += () =>
        {
            StateHasChanged();
        };
        NavigationManager.LocationChanged += OnLocationChanged;
        if (Notifier.Items.Count != 0)
        {
            Notifier.Items.Clear();
            StateHasChanged();
        }
    }

    public void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
    }
}
